package de.uni.muenster.idw2.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import de.uni.muenster.idw2.R;
import de.uni.muenster.idw2.data.BBBuilding;
import de.uni.muenster.idw2.listener.RotationListener;
import de.uni.muenster.idw2.threads.LastBuildingImageLoaderThread;
import de.uni.muenster.idw2.view.RotatorImage;

public class LastBuildingsAdapter extends BaseAdapter{

	
	private final ArrayList<BBBuilding> bbBuildings;
	private ArrayList<RotatorImage> rotatorImages = new ArrayList<RotatorImage>();
	private ArrayList<TextView> textViewsDistance = new ArrayList<TextView>();
	private ArrayList<ImageView> imageViewsBuilding = new ArrayList<ImageView>();
	private final Activity act;
	private RotationListener rotationListener;
	private LocationManager locManager;
	private LastBuildingImageLoaderThread loaderThread;

	public LastBuildingsAdapter(Activity act, ArrayList<BBBuilding> bbBuildings, ProgressBar progressBar){
		this.act = act;
		this.bbBuildings = bbBuildings;
		
		rotationListener = new RotationListener(this.bbBuildings, rotatorImages, act, textViewsDistance, imageViewsBuilding);
		rotationListener.enableOrientation();
		locManager = (LocationManager) act.getSystemService(act.LOCATION_SERVICE);
		locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10, rotationListener);
	
		loaderThread = new LastBuildingImageLoaderThread(act, this.bbBuildings, imageViewsBuilding, progressBar);
		loaderThread.execute();
	}
	
	@Override
	public int getCount() {
		return bbBuildings.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item_direction, null);
        }
        BBBuilding bbBuilding = bbBuildings.get(position);
        if (bbBuilding != null) {
                TextView tt = (TextView) v.findViewById(R.id.textViewTable);
                RotatorImage rotate = (RotatorImage) v.findViewById(R.id.rotationView);
                TextView ttDistance = (TextView) v.findViewById(R.id.textViewTableDistance);
                ImageView imageViewBuilding = (ImageView) v.findViewById(R.id.imageViewBuilding);
                
                if (tt != null) {
                      tt.setText(bbBuilding.getName());                           
                      }
                
                if (ttDistance != null) {
                	textViewsDistance.add(ttDistance);
                    } else{
                    textViewsDistance.add(null);	
                    }
                
                if(rotate != null){
                	rotatorImages.add(rotate);
                } else{
                	rotatorImages.add(null);
                }
                
                if(imageViewBuilding != null){
                	imageViewsBuilding.add(imageViewBuilding);
                } else{
                	imageViewsBuilding.add(null);
                }
        }
        return v;
	}
	
	public void stop(){
		rotationListener.disableOrientation();
		locManager.removeUpdates(rotationListener);
		if(loaderThread != null && !loaderThread.isCancelled()){
			loaderThread.cancel(true);
		}
	}

}
